<!--
 * @Author: changwenqiao
 * @Date: 2021-10-23 17:07:43
 * @LastEditors: [yuantongxin]
 * @LastEditTime: 2021-10-25 22:29:10
 * @Description: 
-->
<template>
  <div style="padding: 20px">
    <!--第一行开始 -->
    <div>
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item>
          <el-button type="primary" plain @click="addColumn">添加</el-button>
        </el-form-item>

        <el-form-item>
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="form.status" placeholder="请选择状态" clearable>
            <el-option label="正常" value="正常"></el-option>
            <el-option label="禁用" value="禁用"></el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="toSeacher">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 第一行结束 -->

    <!-- 表格开始 -->
    <div>
      <el-table :data="tableData | customer" class="table-data">
        <el-table-column
          type="index"
          :index="1"
          prop="date"
          label="序号"
          width="80"
        >
        </el-table-column>
        <el-table-column prop="id" label="产品编号" width="150">
        </el-table-column>
        <el-table-column prop="username" label="用户名" width="150">
        </el-table-column>
        <el-table-column prop="realname" label="姓名" width="150">
        </el-table-column>
        <el-table-column prop="telephone" label="手机号" width="150">
        </el-table-column>
        <el-table-column prop="gender" label="性别" width="150">
        </el-table-column>
        <el-table-column prop="status" label="状态" width="150">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.status === '正常'"
              >正常</el-tag
            >
            <el-tag type="danger" v-else>禁用</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="toEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              @click="toDetail(scope.row)"
              style="color: #5282b2"
              >详情</el-button
            >
            <el-button
              type="text"
               @click="remove(scope.row.id)"
              style="color: #e65f70"
              icon="el-icon-delete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 表格结束 -->

    <!-- 分页开始 -->
    <div>
      <el-pagination 
      background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top:20px;text-align: right; "
      >
      </el-pagination>
    </div>
    <!-- 分页结束 -->

    <!-- 模态框开始 -->
    <el-dialog :title="title" :visible.sync="visible">
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="姓名"
          :label-width="formLabelWidth"
          prop="realname"
        >
          <el-input v-model="form.realname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="手机号"
          :label-width="formLabelWidth"
          prop="telephone"
        >
          <el-input v-model="form.telephone" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          label="密码"
          :label-width="formLabelWidth"
          prop="password"
        >
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth" prop="gender">
          <el-radio v-model="radio" label="男">男</el-radio>
          <el-radio v-model="radio" label="女">女</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSubmit('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>

<script>
import { pageQuery,saveOrUpdate,deleteById } from "@/api/customer";

export default {
  data() {
    var checkMobile = (rule, value, cb) => {
      const regMobile = /^1[345789]\d{9}$/;
      if (regMobile.test(value)) {
        return cb();
      }
      //返回一个错误提示
      cb(new Error("请输入合法的手机号码"));
    };
    return {
      form: {},
      tableData: [],
      params: {
        page: 1,
        pageSize: 10,
      },
      radio: "男",
      total: 0,
      title: "",
      visible: false,
      formLabelWidth: "80px",

      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        realname: [
          { required: true, message: "请输入姓名", trigger: "blur" },
        ],
        telephone: [
          { validator: checkMobile, trigger: "blur" },
          { required: true, message: "请输入手机号", trigger: "blur" },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 6,
            message: "长度为6个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //给搜索按钮绑定事件 --- 模糊查询
    async toSeacher() {
      let temp = {
        ...this.params,
        ...this.form,
      };
      for (let key in temp) {
        if (!temp[key]) {
          delete temp[key];
        }
      }
      let res = await pageQuery(temp);
      console.log(res);
      this.tableData = res.data.list;
      this.total = res.data.total;
    },
    toDetail(item){
      this.$router.push({
          path: '/details/index',
          query: item
        })
    },
    remove(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await deleteById({ id });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.queryPage();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //分页数据查询
    async queryPage() {
      //用解构的方法传递参数
      let temp = {
        ...this.params,
      };
      let res = await pageQuery(temp);
      console.log(res);
      this.tableData = res.data.list;
      this.total = res.data.total;
    },
    //处理每页显示的条数
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      // this.queryPage()
    },
    //处理当前页
    handleCurrentChange(page) {
      this.params.page = page;
      // this.queryPage()
    },
    addColumn() {
      //显示模态框
      this.visible = true;
      //改变title
      this.title = "添加顾客信息";
      this.form = {};
    },
    toSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          //校验通过
          let res = await saveOrUpdate(this.form);
          console.log(res);
          this.visible = false;
          this.$message({
            message: res.message,
            type: "success",
          });
          this.queryPage();
        } else {
          this.$message.error("保存失败");
          return false;
        }
      });
    },
    toEdit(row) {
      this.visible = true;
      this.title = "修改顾客信息";
      this.form = { ...row };
      this.imageUrl = this.form.photo;
    },
  },
  created() {
    this.queryPage();
  },
  filters: {
    customer(data) {
      return data.filter((item) => {
        return item.roles[0].name === "顾客";
      });
    },
  },
   watch: {
    params: {
      handler() {
        this.queryPage();
      },
      deep: true,
    },
  },
};
</script>

<style lang="scss" scoped>
</style>